package com.example.sqlitedemo0413;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.sqlitedemo0413.utils.Constant;
import com.example.sqlitedemo0413.utils.DbManager;
import com.example.sqlitedemo0413.utils.MySqliteHelper;

public class MainActivity extends AppCompatActivity {

    private MySqliteHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        helper = DbManager.getIntance(this);//获得数据库帮助类的对象
    }

    //点击按钮创建数据库
    public void createDb(View view){
        /**
         * getReadableDatabase()/getWritableDatabase() 创建或打开数据库
         * 如果数据库不存在则创建数据库/如果数据库存在则打开数据库
         * 默认情况下两个函数都代表打开或者创建可读可写的数据库，
         * 如果磁盘已满或者数据库本身权限等情况下
         * getReadableDatabase()打开的是只读数据库
         */
        SQLiteDatabase db = helper.getWritableDatabase();
    }

    public void click(View view){
        switch (view.getId()){
            case R.id.btn_insert:
                SQLiteDatabase db = helper.getWritableDatabase();
                String sql = "insert into "+Constant.TABLE_NAME+" values(18,'sun',20)";
                DbManager.exeSQL(db,sql);
                String sql2 = "insert into "+Constant.TABLE_NAME+" values(19,'fzg',22)";
                DbManager.exeSQL(db,sql2);
                db.close();
                break;
            case R.id.btn_update:
                db = helper.getWritableDatabase();
                String updateSql = "update "+Constant.TABLE_NAME+" set "+Constant.NAME+"='sun' where "+Constant._ID+"=18";
                DbManager.exeSQL(db,updateSql);
                db.close();
                break;
            case R.id.btn_delete:
                db = helper.getReadableDatabase();
                String delSql="delete from "+Constant.TABLE_NAME+" where "+Constant._ID+"=19";
                DbManager.exeSQL(db,delSql);
                db.close();
                break;
        }

    }

    public void onclick(View view){
        switch (view.getId()){
            case R.id.btn_insertApi:
                SQLiteDatabase db = helper.getReadableDatabase();
                /**
                 * (String table, String nullColumnHack, ContentValues values)
                 * String table  表示插入数据表的名称
                 * String nullColumnHack
                 * ContentValues values  键为String类型的hashmap集合
                 * 返回值 long 表示插入数据的列数
                 */
                ContentValues values = new ContentValues();
                values.put(Constant._ID,13);
                values.put(Constant.NAME,"孙红霞");
                values.put(Constant.AGE,30);
                long result = db.insert(Constant.TABLE_NAME,null,values);
                if(result>0){
                    Toast.makeText(MainActivity.this,"插入数据成功！",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(MainActivity.this,"插入数据失败！",Toast.LENGTH_LONG).show();
                }
                db.close();
                break;
            case R.id.btn_updateApi:
                db = helper.getReadableDatabase();
                /**
                 * String table, ContentValues values, String whereClause, String[] whereArgs
                 * String table  表示修改的数据表的名称
                 * ContentValues values  表示键为String类型的hashmap
                 * String whereClause   表示修改条件
                 * String[] whereArgs   表示修改条件的占位符
                 * count  返回值表示修改的条数
                 */
                ContentValues values1 = new ContentValues();
                values1.put(Constant.NAME,"小孙"); //put(需要修改的字段名称，修改后的字段值)
               // int count = db.update(Constant.TABLE_NAME, values1, Constant._ID+"=3", null);
                int count = db.update(Constant.TABLE_NAME, values1, Constant._ID+"=?", new String[]{"3"});
                if(count>0){
                    Toast.makeText(MainActivity.this,"修改数据成功！",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(MainActivity.this,"修改数据失败！",Toast.LENGTH_LONG).show();
                }
                db.close();
                break;
            case R.id.btn_deleteApi:
                db = helper.getReadableDatabase();
                /**
                 * String table, String whereClause, String[] whereArgs
                 * String table 表示删除的表名
                 * String whereClause   表示删除的条件
                 * String[] whereArgs   表示删除条件的占位符
                 */
                int count2 = db.delete(Constant.TABLE_NAME, Constant._ID+"=?", new String[]{"5"});
                if(count2>0){
                    Toast.makeText(MainActivity.this,"删除数据成功！",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(MainActivity.this,"删除数据失败！",Toast.LENGTH_LONG).show();
                }
                db.close();
                break;
            case R.id.btn_queryApi:
                db = helper.getReadableDatabase();
                Cursor cursor =  db.query(Constant.TABLE_NAME,null,null,null,null,null,null);
                if(cursor.moveToFirst()){
                    do{
                        //遍历Cursor对象，取出数据并打印
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String age = cursor.getString(cursor.getColumnIndex("age"));
                        Log.i("tag","name is" + name);
                        Log.i("tag","age is" + age);
                    }while (cursor.moveToNext());
                }

            }
    }



}